#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setLayout(ui->verticalLayout);
    ui->widgetBottom->setLayout(ui->horizontalLayout_2);
    _encode = {{"Utf8", QStringConverter::Utf8}, {"Utf16", QStringConverter::Utf16},
               {"Utf16LE", QStringConverter::Utf16LE}, {"Utf16BE", QStringConverter::Utf16BE},
               {"Utf32", QStringConverter::Utf32}, {"Utf32LE", QStringConverter::Utf32LE},
               {"Utf32LE", QStringConverter::Utf32LE}, {"Utf32BE", QStringConverter::Utf32BE},
               {"Latin1", QStringConverter::Latin1}, {"System", QStringConverter::System} };

    QShortcut* openShortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), this);
    QShortcut* saveShortcut = new QShortcut(QKeySequence(tr("Ctrl+S", "File|Save")), this);

    QShortcut* zommInShortcut = new QShortcut(QKeySequence(tr("Ctrl+Shift+=")), this);
    QShortcut* zommOutShortcut = new QShortcut(QKeySequence(tr("Ctrl+Shift+-")), this);

    connect(openShortcut, &QShortcut::activated, [this]() {
        on_btnOpen_clicked();
    });
    connect(saveShortcut, &QShortcut::activated, [this]() {
        on_btnSave_clicked();
    });

    connect(zommInShortcut, &QShortcut::activated, [this]() {
        QFont qfont = ui->textEdit->font();
        int fontSize = qfont.pointSize();
        qfont.setPointSize(++fontSize);
        ui->textEdit->setFont(qfont);
    });
    connect(zommOutShortcut, &QShortcut::activated, [this]() {
        QFont qfont = ui->textEdit->font();
        int fontSize = qfont.pointSize();
        qfont.setPointSize(--fontSize);
        ui->textEdit->setFont(qfont);
    });

    connect(ui->comboBox, &QComboBox::currentIndexChanged, this, &Widget::on_qCombox_changed);
    connect(ui->textEdit, &QTextEdit::cursorPositionChanged, this, &Widget::on_current_position_changed);
}

Widget::~Widget()
{
    delete ui;
    if (_file.isOpen()) {
        _file.close();
    }
    _encode.clear();
}

void Widget::on_btnOpen_clicked()
{
    if (_file.isOpen()) {
        _file.close();
    }
    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                    "D:/code/qtcode",
                                                    tr("Text files (*.txt)"));
    ui->textEdit->clear();
    _file.setFileName(filename);
    if (!_file.open(QIODeviceBase::ReadWrite | QIODeviceBase::Text)) {
        qDebug() << "open file failed";
    }
    setWindowTitle("我的记事本：" + filename);
    QTextStream in(&_file);
    while (!in.atEnd()) {
        ui->textEdit->append(in.readLine());
    }
}


void Widget::on_btnSave_clicked()
{
    if (!_file.isOpen()) {
        QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                                                        "D:/code/qtcode/default.txt",
                                                        tr("Text files (*.txt *.cpp)"));
        _file.setFileName(fileName);
        if (!_file.open(QIODeviceBase::WriteOnly | QIODeviceBase::Text)) {
            qDebug() << "open file failed";
        }
        setWindowTitle("我的记事本：" + fileName);
    }
    _file.resize(0);
    QTextStream out(&_file);
    out.setEncoding(QStringConverter::Utf8);
    out << ui->textEdit->toPlainText();
}


void Widget::on_btnClose_clicked()
{
    QMessageBox msgBox;
    msgBox.setText("The document has been modified.");
    msgBox.setInformativeText("Do you want to save your changes?");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();

    switch (ret) {
    case QMessageBox::Save:
        on_btnSave_clicked();
        break;
    case QMessageBox::Discard:
        ui->textEdit->clear();
        if (_file.isOpen()) {
            _file.close();
            setWindowTitle("我的记事本");
        }
        break;
    case QMessageBox::Cancel:
        // Cancel was clicked
        break;
    default:
        // should never be reached
        break;
    }
}


void Widget::on_qCombox_changed()
{
    if (!_file.isOpen()) {
        return;
    }
    _file.seek(0);
    ui->textEdit->clear();
    QTextStream in(&_file);
    in.setEncoding(_encode[ui->comboBox->currentText()]);
    while (!in.atEnd()) {
        ui->textEdit->append(in.readLine());
    }
}

void Widget::on_current_position_changed()
{
    QTextCursor cursor = ui->textEdit->textCursor();
    //设置当前行列信息
    ui->labelPoisition->setText(QString::number(cursor.blockNumber() + 1) + ":" + QString::number(cursor.columnNumber() + 1));

    //设置高亮
    QList<QTextEdit::ExtraSelection> list;
    QTextEdit::ExtraSelection extraSelection;

    QTextCharFormat format;
    QBrush qbrush(Qt::yellow);
    //设置背景色
    format.setBackground(qbrush);
    //设置下划线
    format.setFontUnderline(true);
    //这句话必须得有
    format.setProperty(QTextFormat::FullWidthSelection, true);

    extraSelection.cursor = cursor;
    extraSelection.format = format;
    list.push_back(extraSelection);

    ui->textEdit->setExtraSelections(list);
}



